{% extends 'project_python/layout_python.html' %}
{% load static %}

{% block panel_js_files %}
<script src="{% static 'js/sharedkit.js' %}"></script>
{% endblock %}

{% block html_js_code %}
<script>
layui.use(function(){
    var $ = layui.$;
    var layer = layui.layer;
    var util = layui.util;
    var form = layui.form;

    util.on("lay-on", {     
        "open_explorer": function(obj){
            var url = $(this).data('url');
            openFolder(url);
            return false;
        },
        "openTerminal": function(obj){
            var url = obj.data('url');
            openTerminal(url);
            return false;
        }, 
        openproject: function(){
          const csrftoken = getCookie('csrftoken');
            var loadIndex = layer.msg('使用 Pycharm 打开项目中...', {icon: 16,shade: 0.6, title:null});
            $.ajax({
                url: "{% url 'project_python:pycharm_project_open' %}",
                type: "POST",
                headers: {'X-CSRFToken': csrftoken},
                mode: 'same-origin',
                data:{
                  'path':$(this).data('path'),
                },
                success:function(response){
                    loadIndex.close();
                },
                error: function(xhr, status, message){
                    let errorMsg = '操作失败: ';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMsg += xhr.responseJSON.message;
                    } else {
                        errorMsg += message;
                    }
                    layer.alert(errorMsg, {icon: 2, title:null});
                }
            })
        },
        runPycharm: function(){       
            layer.msg('启动Pycharm,注意屏幕...', {icon: 16,shade: 0.6, title:null});    
            $.ajax({
                url: $(this).data('href'),
                type: "get",
                success:function(response){},
                error: function(xhr, status, message){
                    let errorMsg = '操作失败: ';
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        errorMsg += xhr.responseJSON.message;
                    } else {
                        errorMsg += message;
                    }
                    layer.alert(errorMsg, {icon: 2, title:null});
                }
            })
        },
    })

})
</script>
{% endblock %}


{% block python_content %}
<div class="text-danger text-bold col-md-12 p-2" style="width:100%;text-align: center;">对Pycharm操作请保持Pycharm处于关闭状态！</div>
<div class="card-header">
    <div class="card-tools">
    <a href="{% url 'project_python:software' %}" class="btn btn-outline-info"><i class="fa-solid fa-wrench"></i>其他 Python 开发工具</a>
    </div>
</div>
{% include 'include_messages.html' %}
<div class="card">
    <div class="card-header d-flex p-0">
        <div class="card-title p-1">
            <a href="{% url 'project_python:project_create' %}" class="btn btn-success"><i class="fa fa-plus"></i> 添加项目 </a>
        </div>

        <ul class="nav nav-pills ml-auto p-2 mr-5">
            <li class="nav-item"><a class="nav-link active" href="{% url 'project_python:index' %}">Pycharm项目</a></li>
            <li class="nav-item ml-1"><a class="nav-link" href="{% url 'project_python:pycharm_sdk_list' %}">Pycharm解释器</a></li>
            <li class="nav-item dropdown ml-1">
            <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
                Pycharm管理 <span class="caret"></span>
            </a>
            <div class="dropdown-menu">
                <a class="dropdown-item" tabindex="-1" href="#" data-href="{% url 'project_python:pycharm_run' 'normal' %}" lay-on="runPycharm">启动(默认)</a>
                <a class="dropdown-item" tabindex="-1" href="#" data-href="{% url 'project_python:pycharm_run' 'list' %}" lay-on="runPycharm">启动(从项目列表)</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" tabindex="-1" href="{% url 'project_python:pycharm_reset' %}">重置Pycharm</a>
                <div class="dropdown-divider"></div>
                <a class="dropdown-item" tabindex="-1" href="{%  url 'project_python:pycharm_detail' %}">版本信息/卸载</a>
            </div>
            </li>
        </ul>

    </div>
    <div class="card-body p-2">
{% if projects %}
  {% for project_path, info in projects.items %}
<div class="row mb-0" style="border-bottom:1px solid #e9ecef;">
      <table class="table table-borderless table-hover mb-0">
        <tbody>
            <tr>
              <td>
                  <div class="text-bold layui-font-18">
{% if info.project_path_exists %}
<button class="btn btn-outline-secondary btn-xs"
        data-url="{% url 'sharedkit:open_explorer' %}?path={{ info.project_path }}"
        title="打开文件夹" lay-on="open_explorer">
    <i class="far fa-folder-open"></i>
</button>
<button type="button" data-url="{% url 'sharedkit:open_terminal' %}?path={{ info.project_path }}"
        class="btn btn-outline-secondary btn-xs" title="打开终端命令行" lay-on="openTerminal">
    <i class="fa fa-terminal fa-xs"></i>
</button>
{% endif %}
{{ info.project_name }}</div>
                  <div class="text-muted">
                    项目位置：{{ project_path }}
                    {% if not info.project_path_exists %}<i class="fa-solid fa-circle-exclamation text-danger" title="路径异常"></i>{% endif %}
                </div>
                  <div class="text-muted">环境名称：{{ info.sdk_name }}</div>
                  <div class="text-muted">Python版本：{{ info.sdk_version }}</div>
                  <div class="text-muted">
                    解释器路径：{{ info.sdk_path }}
                    {% if not info.sdk_path_exists %}<i class="fa-solid fa-circle-exclamation text-danger" title="路径异常"></i>{% endif %}
                </div>
              </td>
              <td style="width:120px;" class="p-1">

    <button type="button" data-path="{{ info.project_path }}" class="btn btn-outline-success btn-xs btn-block" lay-on="openproject">
    在Pycharm打开
    </button>

    <a href="{% url 'project_python:project_set_sdk' %}?path={{ project_path }}" class="btn btn-outline-info btn-xs btn-block mt-1">配置解释器</a>

    <form action="{% url 'project_python:project_del' %}" method="post">{% csrf_token %}
        <input type="hidden" id="project_path" name="project_path" value="{{ project_path }}" />
    <button type="submit" id="form_btn_submit" name="form_btn_submit" class="btn btn-outline-danger btn-xs btn-block mt-1" 
            onclick="return confirm('删除将只删除Pycharm里的项目记录~不会删除真实的代码~\r确定要删除吗？');">删除</button>
    </form>
              </td>
            </tr>
        </tbody>
      </table>
</div>
  {% endfor %}
  {% else %} 没有项目信息 {%endif%}
    </div>
</div>

{% endblock %}